package com.jt.www.remote.woxuebao;

import com.jt.www.model.reps.GenericDataResponse;
import com.jt.www.model.vo.OfflineIssueQo;
import com.jt.www.model.woxuebao.TOrderChargeQueryVO;
import config.FeignConfiguration;
import feign.hystrix.FallbackFactory;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * 投保中心 定单相关
 */
@FeignClient(value = "ins-order", url = "${remote.ins_order_url}", configuration = FeignConfiguration.class, fallbackFactory = InsOrderClient.FeignClientFallbackFactory.class)
public interface InsOrderClient {


    @PostMapping(value = "/orderPay",
            produces = MediaType.APPLICATION_JSON_UTF8_VALUE,
            consumes = MediaType.APPLICATION_JSON_UTF8_VALUE)
    ResponseEntity<GenericDataResponse<Map<String, String>>> orderPay(@RequestBody TOrderChargeQueryVO param) throws Exception;


    @PostMapping(value = "/order/submitOrder",
            produces = MediaType.APPLICATION_JSON_UTF8_VALUE,
            consumes = MediaType.APPLICATION_JSON_UTF8_VALUE)
    ResponseEntity<GenericDataResponse<Map<String, Object>>> submitOrder(@RequestBody String json) throws Exception;

    @RequestMapping(value = "/order/eduOfflineIssueBatch", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
    @ApiOperation(value = "线下补录出单接口", notes = "{userCode:用户编码,appCode:投保单号,policyNo:保单号,issueDate:签单日期,startDate:保险起期,endDate:保险止期,attachments:附件集合}")
    ResponseEntity<GenericDataResponse<Map<String, Object>>> eduOfflineIssueBatch(@RequestBody List<OfflineIssueQo> issueQoList) throws Exception;


    @RequestMapping(value = "/order/submitOrderOffline/v2/{channelCode}", produces = MediaType.APPLICATION_JSON_UTF8_VALUE, consumes = "application/json;charset=UTF-8")
    @ApiOperation(value = "线下录入")
    ResponseEntity<GenericDataResponse<Map<String, Object>>> submitOrderOffline(@RequestBody String json,
                                                                                @RequestParam(value = "channelCode") String channelCode) throws Exception;


    @PostMapping(value = "/dataExchange/policyAgent/{appCode}", produces = MediaType.APPLICATION_JSON_UTF8_VALUE, consumes = "application/json;charset=UTF-8")
    @ApiOperation(value = "保单绑定推广人")
    GenericDataResponse policyAgents(@RequestBody String json, @RequestParam(value = "appCode") String appCode) throws Exception;


    @Component
    class FeignClientFallbackFactory implements FallbackFactory<InsOrderClient> {

        public static final Logger log = LoggerFactory.getLogger(FeignClientFallbackFactory.class);

        @Override
        public InsOrderClient create(Throwable cause) {
            return new InsOrderClient() {

                @Override
                public ResponseEntity<GenericDataResponse<Map<String, String>>> orderPay(TOrderChargeQueryVO param) throws Exception {
                    log.error("投保中心接收支付网关支付结果的接口调用失败,fallback reason:", cause);
                    return null;
                }

                @Override
                public ResponseEntity<GenericDataResponse<Map<String, Object>>> submitOrder(String json) throws Exception {
                    log.error("投保接口调用失败,fallback reason:", cause);
                    return null;
                }

                @Override
                public ResponseEntity<GenericDataResponse<Map<String, Object>>> eduOfflineIssueBatch(List<OfflineIssueQo> issueQoList) throws Exception {
                    log.error("线下补录出单接口,fallback reason:", cause);
                    return null;
                }

                @Override
                public ResponseEntity<GenericDataResponse<Map<String, Object>>> submitOrderOffline(String json, String channelCode) throws Exception {
                    log.error("线下录入接口,fallback reason:", cause);
                    return null;
                }

                @Override
                public GenericDataResponse policyAgents(String json, String appCode) throws Exception {
                    log.error("保单绑定推广人,fallback reason:", cause);
                    return null;
                }

            };
        }
    }
}
